<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="旅行社" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入旅行社名称" />
                </el-form-item>
                <el-form-item label="营业执照" prop="licence">
                    <material-picker v-model="formData.licence" />
                </el-form-item>
                <el-form-item label="经营许可证" prop="business_licence">
                    <material-picker v-model="formData.business_licence" />
                </el-form-item>
                <el-form-item label="联系电话" prop="contact_phone">
                    <el-input
                        v-model="formData.contact_phone"
                        clearable
                        placeholder="请输入联系电话"
                    />
                </el-form-item>
                <el-form-item label="联系人" prop="contact_name">
                    <el-input
                        v-model="formData.contact_name"
                        clearable
                        placeholder="请输入联系人"
                    />
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input v-model="formData.address" clearable placeholder="请输入地址" />
                </el-form-item>
                <el-form-item label="简介" prop="desc">
                    <editor class="flex-1" v-model="formData.desc" :height="500" />
                </el-form-item>
                <el-form-item label="佣金比例" prop="brokerage_percent">
                    <el-input
                        v-model="formData.brokerage_percent"
                        clearable
                        placeholder="请输入佣金比例"
                    />
                </el-form-item>
                <el-form-item label="是否禁用" prop="status">
                    <el-radio-group v-model="formData.status" placeholder="请选择是否禁用">
                        <el-radio :value="0">正常</el-radio>
                        <el-radio :value="1">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        <div class="form-tips">默认为0， 数值越大越排前</div>
                    </div>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="travelAgencyEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiTravelAgencyAdd, apiTravelAgencyEdit, apiTravelAgencyDetail } from '@/api/travel_agency'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑旅行社表' : '新增旅行社表'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    licence: '',
    business_licence: '',
    contact_phone: '',
    contact_name: '',
    address: '',
    desc: '',
    travel_money: '',
    brokerage_percent: '',
    sort: 0,
    status: 0
})

// 表单验证
const formRules = reactive<any>({
    name: [
        {
            required: true,
            message: '请输入旅行社名称',
            trigger: ['blur']
        }
    ],
    licence: [
        {
            required: true,
            message: '请选择营业执照',
            trigger: ['blur']
        }
    ],
    business_licence: [
        {
            required: true,
            message: '请选择经营许可证',
            trigger: ['blur']
        }
    ],
    contact_phone: [
        {
            required: true,
            message: '请输入联系电话',
            trigger: ['blur']
        }
    ],
    contact_name: [
        {
            required: true,
            message: '请输入联系人',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择是否禁用',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiTravelAgencyDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiTravelAgencyEdit(data) : await apiTravelAgencyAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
